package DAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Model.AvaliacaoAntropometrica;
import Utils.Conexao;

import com.mysql.jdbc.CallableStatement;

public class AvaliacaoAntropometricaDAO {	

		public static void Insert(AvaliacaoAntropometrica objAvAntrop) throws SQLException
		{
			Connection con = Conexao.getConection();		
			String mysqlCall = "call spc_insert_avaliacao_antropometrica(?, ?, ?, ?, ?, ?)";				
			CallableStatement call = AvaliacaoAntropometricaDAO.SetObjectCallableStatement(objAvAntrop, mysqlCall, con);
			call.executeUpdate();
			con.close();		
			call.close();
		}
		
		public static void Update(AvaliacaoAntropometrica objAvAntrop) throws SQLException
		{		
			Connection con = Conexao.getConection();		
			String mysqlCall = "call spc_update_avaliacao_antropometrica(?, ?, ?, ?, ?, ?, ?)";
			
			CallableStatement call = AvaliacaoAntropometricaDAO.SetObjectCallableStatement(objAvAntrop, mysqlCall, con) ;
			call.executeUpdate();
			con.close();
			call.close();
		}		
		
		public static ResultSet relatorioAvaliacaoAntropometrica(java.sql.Date data_inicio, java.sql.Date data_fim) throws SQLException
		{
			Connection con = Conexao.getConection();		
			String mysqlCall = "call spc_relatorio_avaliacao_antropometrica(?, ?)";
			CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
			call.setDate(1, data_inicio);
			call.setDate(2, data_fim);		

			return call.executeQuery();
		}
		
		public static AvaliacaoAntropometrica LoadObject(int id) throws SQLException
		{		
			AvaliacaoAntropometrica avaliacao = null;
			Connection con = Conexao.getConection();
			String mysqlCall = "call spc_select_avaliacao_antropometrica_por_id(?)";			
			CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
			call.setInt(1, id);
			ResultSet res = call.executeQuery();
			
			if(res.next())
				avaliacao = AvaliacaoAntropometricaDAO.SetInstance(res);		
			
			con.close();
			call.close();
			
			return avaliacao;
		}
		
		public static ArrayList<AvaliacaoAntropometrica> GetArrayListAvaliacaoAntropometricas()
		{
			Connection con = Conexao.getConection(); 
			ArrayList<AvaliacaoAntropometrica> arrAvaliacaoAntropometrica = new ArrayList<AvaliacaoAntropometrica>();
			try
			{
				String mysqlCall = "call spc_select_avaliacoes_antropometricas()";				
				CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
				ResultSet res = call.executeQuery();
				while(res.next())
				{
					AvaliacaoAntropometrica avaliacao =  AvaliacaoAntropometricaDAO.SetInstance(res);
					arrAvaliacaoAntropometrica.add(avaliacao);
				}			
				call.close();
				con.close();			
			}
			catch(SQLException ex)
			{
				ex.printStackTrace();
			}
			
			return arrAvaliacaoAntropometrica;
		}
		
		public static void Delete(int id) throws SQLException
		{
			Connection con = Conexao.getConection();
			String mysqlCall = "call spc_inativar_avaliacao_antropometrica(?)";			
			CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
			call.setInt(1, id);
			call.execute();
			con.close();
			call.close();
		}
		
		private static CallableStatement SetObjectCallableStatement(AvaliacaoAntropometrica objAvAntrop, String mysqlCall, Connection con ) throws SQLException
		{			
			CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
			call.setDate(1, objAvAntrop.getDataAvaliacao());
			call.setDouble(2, objAvAntrop.getPeso());
			call.setDouble(3, objAvAntrop.getAltura());
			call.setInt(4,   objAvAntrop.getPressaoArterial());
			call.setDouble(5, objAvAntrop.getMedidasCorporeas());
			call.setInt(6, objAvAntrop.getIdPaciente());
			
			if(objAvAntrop.getIdAvaliacaoAntropometrica() > 0)
				call.setInt(7, objAvAntrop.getIdAvaliacaoAntropometrica());
		
			return call; 		
		}	

		
		public static AvaliacaoAntropometrica SetInstance(ResultSet res) throws SQLException
		{
			AvaliacaoAntropometrica avaliacao = new AvaliacaoAntropometrica();		
			avaliacao.setIdAvaliacaoAntropometrica(res.getInt("id_avaliacao_antropometrica"));
			avaliacao.setDataAvaliacao(res.getDate("data_avaliacao"));
			avaliacao.setPeso(res.getDouble("peso"));
			avaliacao.setAltura(res.getDouble("altura"));
			avaliacao.setPressaoArterial(res.getInt("pressao_arterial"));
			avaliacao.setMedidasCorporeas(res.getDouble("medidas_corporeas"));
			avaliacao.setIdPaciente(res.getInt("id_paciente"));
			avaliacao.setPaciente(PacienteDAO.LoadObject(res.getInt("id_paciente")));
			
			return avaliacao;		
		}
}
